package cn.lfzj.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.lfzj.dao.BookDao;
import cn.lfzj.dao.PubDao;
import cn.lfzj.model.Book;
import cn.lfzj.model.Pub;
import cn.lfzj.model.User;

@WebServlet("/book.do")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			response.sendRedirect("index.jsp");
			return;
		}

		String type = request.getParameter("type");
		if ("".equals(type) || type != null) {
			switch (type) {
			case "gotoadd":
				PubDao pubDao = new PubDao();
				List<Pub> pubList = pubDao.selectAll();
				request.setAttribute("pubList", pubList);
				request.getRequestDispatcher("/WEB-INF/jsp/add.jsp").forward(request, response);
				break;
			case "gotoindex":
				request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
				break;
			default:
				break;
			}
			return;
		}

		String action = request.getParameter("action");
		if ("".equals(action) || action == null) {
			action = "query";
		}
		switch (action) {
		case "query":
			this.query(request, response);
			break;
		case "add":
			this.add(request, response);
			break;
		case "deletebyid":
			this.deleteById(request, response);
			break;
		case "delete":
			this.delete(request, response);
			break;
		case "querybyid":
			this.queryById(request, response);
			break;
		case "update":
			this.update(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bookName = request.getParameter("bookname");
		BookDao dao = new BookDao();
		List<Book> list = dao.select(bookName);
		request.getSession().setAttribute("bookName", bookName);
		request.setAttribute("list", list);
		request.getRequestDispatcher("/WEB-INF/jsp/show.jsp").forward(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bookName = request.getParameter("bookName");
		String author = request.getParameter("author");
		String pub = request.getParameter("pub");
		double price = 0;
		String priceParam = request.getParameter("price");
		if (priceParam != null) {
			price = Double.parseDouble(priceParam);
		}
		Book book = new Book();
		book.setBookName(bookName);
		book.setAuthor(author);
		book.setPub(pub);
		book.setPrice(price);
		BookDao dao = new BookDao();
		if (dao.insert(book) > 0) {
			request.setAttribute("state", 3);
			request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/jsp/failure.jsp").forward(request, response);
		}
	}

	private void deleteById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BookDao dao = new BookDao();

		String idParam = request.getParameter("id");
		int id = 0;
		if (idParam != null) {
			id = Integer.parseInt(idParam);
		}
		dao.deleteById(id);

		String bookname = (String) request.getSession().getAttribute("bookName");
		response.sendRedirect("book.do?bookname=" + bookname);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BookDao dao = new BookDao();
		String[] ids = request.getParameterValues("ids");
		dao.delete(ids);
		String bookname = (String) request.getSession().getAttribute("bookName");
		response.sendRedirect("book.do?bookname=" + bookname);

	}

	private void queryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String idParam = request.getParameter("id");
		int id = 0;
		if (idParam != null) {
			id = Integer.parseInt(idParam);
		}
		BookDao dao = new BookDao();
		Book book = dao.selectById(id);
		request.setAttribute("book", book);
		PubDao pubDao = new PubDao();
		List<Pub> pubList = pubDao.selectAll();
		request.setAttribute("pubList", pubList);
		request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request, response);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bookName = request.getParameter("bookName");
		String author = request.getParameter("author");
		String pub = request.getParameter("pub");
		double price = 0;
		String priceParam = request.getParameter("price");
		if (priceParam != null) {
			price = Double.parseDouble(priceParam);
		}
		String idParam = request.getParameter("id");
		int id = 0;
		if (idParam != null) {
			id = Integer.parseInt(idParam);
		}
		Book book = new Book(id, bookName, author, pub, price);
		BookDao dao = new BookDao();
		dao.update(book);
		String bookname = java.net.URLEncoder.encode((String) request.getSession().getAttribute("bookName"),"UTF-8");
		response.sendRedirect("book.do?bookname=" + bookname);
	}

}
